package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.ArticleRankDao;
import com.qdlc.p2p.dal.dto.Article;
import com.qdlc.p2p.dal.dto.ArticleRank;

/**
 * Created on 2016/1/11.
 */
@Repository("articleRankDao")
public class ArticleRankDaoImpl extends BaseDaoImpl<ArticleRank> implements ArticleRankDao {

    @Override
    public List<Article> rankList(String type) {
        if (StringUtil.isBlank(type)) return null;
        String sql = "SELECT * from " + Global.DB_PREFIX + "article a LEFT JOIN " + Global.DB_PREFIX + "article_rank r on a.id = r.article_id where a.status = 1 and a.site_id IN (9, 14) ORDER BY r." + type + " DESC, a.add_time DESC LIMIT 6";
        Query query = em.createNativeQuery(sql, Article.class);
        return  query.getResultList();
    }

    @Override
    public ArticleRank findByArticleId(long articleId) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("article.id", articleId);
        return super.findByCriteriaForUnique(param);
    }
}
